package com.easyfun.healthmagicbox.widget;

import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import junit.framework.Assert;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.res.AssetFileDescriptor;
import android.widget.EditText;
import android.widget.Toast;


public class PersonProfileItemDialogUtil implements
		DialogInterface.OnMultiChoiceClickListener {
	Context mContext = null;
	ArrayList<String> items = new ArrayList<String>();
	ArrayList<String> values = new ArrayList<String>();
	Vector<Boolean> selected = new Vector<Boolean>();
	String itemsInStr = "";
	String valuesInStr = "";
	EditText edit;
	Dialog dialog = null;
	
	public PersonProfileItemDialogUtil(Context context, ArrayList<String> items,EditText edit) {
		mContext = context;
		this.items = items;
		this.values = items;
		for (int i = 0; i < items.size(); i++) {
			selected.add(false);			
		}		
		this.edit = edit;
	}
	
	public PersonProfileItemDialogUtil(Context context, ArrayList<String> items, 
			ArrayList<String> values, EditText edit) 
	{
		mContext = context;
		Assert.assertEquals(items.size(), values.size());
		this.items = items;
		this.values = values;
		for (int i = 0; i < items.size(); i++) {
			selected.add(false);			
		}		
		this.edit = edit;
	}


	public void createDialog() {
		if (items.size() == 0) {
			Toast.makeText(mContext, "您还没有健康数据呀！",
					Toast.LENGTH_SHORT).show();
			return;
		}
		
		Builder builder = new AlertDialog.Builder(mContext);		
		builder.setTitle("请选择");
		builder.setIcon(android.R.drawable.ic_dialog_info);
		DialogInterface.OnMultiChoiceClickListener mutiListener = this;

		CharSequence[] typenames = new CharSequence[items.size()];
		for (int i = 0; i < items.size(); i++) {
			typenames[i] = items.get(i);
		}
		builder.setMultiChoiceItems(typenames, toPrimitiveArray(selected),
				mutiListener);

		DialogInterface.OnClickListener btnListener = new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialogInterface, int which) {
				itemsInStr = "";
				valuesInStr = "";
				for (int i = 0; i < items.size(); i++) {
					if (selected.get(i))
					{
						itemsInStr = itemsInStr + items.get(i) + ",";
						valuesInStr = valuesInStr + values.get(i) + ",";
					}
				}
				itemsInStr = removeLastComma(itemsInStr);
				valuesInStr = removeLastComma(valuesInStr);
				edit.setText(itemsInStr);
				dialog.dismiss();
			}
		};
		builder.setPositiveButton("确定", btnListener);
		dialog = builder.create();
		dialog.show();
		
		return;
	}

	@Override
	public void onClick(DialogInterface dialogInterface, int which,
			boolean isChecked) {
		selected.set(which, isChecked);
	}

	private boolean[] toPrimitiveArray(final List<Boolean> booleanList) {
		final boolean[] primitives = new boolean[booleanList.size()];
		int index = 0;
		for (Boolean object : booleanList) {
			primitives[index++] = object;
		}
		return primitives;
	}

	public String getValuesInStr() {
		return valuesInStr;
	}
	public String getItemsInStr() {
		return itemsInStr;
	}
	public void setValuesInStr(String ivaluesInStr) {
		valuesInStr = ivaluesInStr;
		String[] valueitems = ivaluesInStr.split(",");
		for(String value:valueitems)
		{
			if(value.length() == 0) continue;
			for (int i=0;i<values.size();i++)
			{
				if (value.equals(values.get(i)))
					selected.set(i, true);
			}
		}
		for (int i = 0; i < items.size(); i++) {
			if (selected.get(i))
			{
				itemsInStr = itemsInStr + items.get(i) + ",";				
			}			
		}
		itemsInStr = removeLastComma(itemsInStr);
	}
	
	private String removeLastComma(String str)
	{
		if (str.length() == 0)
			return str;
		if (str.charAt(str.length()-1) == ',')
		{
			return str.substring(0,str.length()-1);
		}
		return str;
	}
}
